
<template>
  <h2>编辑资讯页面</h2>
  <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">

    <el-form-item label="资讯标题" prop="title">
      <el-input   placeholder="请输入标题" v-model="ruleForm.title"></el-input>
    </el-form-item>

    <el-form-item label="资讯内容" prop="desc">
      <el-input placeholder="请输入正文..." type="textarea" v-model="ruleForm.desc"></el-input>
    </el-form-item>

    <el-form-item>
      <el-button type="primary" @click="open('ruleForm')">提交审核</el-button>
      <el-button @click="resetForm('ruleForm')">重置</el-button>
    </el-form-item>

  </el-form>
</template>

<script>
import {ElMessage} from "element-plus";

export default {
  name: 'Edit',
  inject:['reload'],
  data() {
    return {
      ruleForm: {
        title: '',
        desc: ''
      },
      rules: {
        title: [
          { required: true, message: '请输入资讯标题', trigger: 'blur' },
        ],
        desc: [
          { required: true, message: '输入文章内容', trigger: 'blur' }
        ]
      }
    };
  },
  methods: {
    open(formName) {
      this.$confirm('是否确认操作?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.submitForm(formName)
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          let that = this;
          this.$axios({
            method: "post",
            url: "/articles/editArticles",
            data: {
              userId:JSON.parse(window.sessionStorage.getItem('user')).userId,
              articleTitle:this.$data.ruleForm.title,
              articleContent:this.$data.ruleForm.desc,
            },
          }).then(function (response) {
            // console.log(response);
            if(response.data.code === 1) {
              ElMessage.success({
                message: '资讯提交成功!',
                type: 'success'
              });
              that.$router.push('/list/1');
              that.reload;
            }else {
              ElMessage.error('资讯提交失败!');
            }

          }).catch(function (error) {
            console.log(error);
            ElMessage.error(error.message);
          });

        } else {
          ElMessage.error('error submit!!');
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$confirm('是否确认重置?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.$refs[formName].resetFields();
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });
      });
    }
  }
}
</script>

<style scoped>

</style>